package com.et114.modules.org.dao;

import java.util.List;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.vo.EmpTitleRelVo;
import com.et114.modules.org.vo.GroupVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.UserEmplyVo;

public class EmpDao extends BaseDAOImpl {
	public Long createEmp ( UserEmplyVo userEmplyVo ) throws CheckException {
		Long result = ( Long ) insert ( userEmplyVo , "createEmp" );
		if ( AppContext.isHasLog (  ) ) {	
			userEmplyVo.setOperType ( Constants.LOG_OPER_NEW );
			addLogAuthEmply ( userEmplyVo );
		}
		return result;
	}
	
	public List queryEmpWithTitleList ( GroupVo groupVo ) throws CheckException {
		return queryList ( groupVo , "queryEmpWithTitleList" );
	}
	
	public Long countEmpWithTitles ( GroupVo groupVo ) throws CheckException {
		return getCount ( groupVo , "countqueryEmpWithTitles" );
	}	
	
	public List queryEmpsOfGroup ( GroupVo groupVo ) throws CheckException {
		return queryList ( groupVo , "queryEmpsOfGroup" );
	}
	
	public List queryEmpByUser ( Long userId ) throws CheckException {
		return queryList ( userId , "queryEmpByUser" );
	}
	
	public UserEmplyVo loadEmpByUser ( Long userId ) throws CheckException {
		// Object object = load(userId, "loadEmpByUser");
		// return object != null ? (UserEmplyVo) object : null;
		
		return ( UserEmplyVo ) load ( userId , "loadEmpByUser" );
	}
	
	public Long countEmpCode ( String empCode ) throws CheckException {
		return getCount ( empCode , "countEmpByEmpCode" );
	}
	
	public Object loadEmpByEmpCode ( String empCode ) throws CheckException {
		return ( UserEmplyVo ) load ( empCode , "loadEmpByEmpCode" );
	}
	
	public UserEmplyVo loadEmp ( Long empId ) throws CheckException {
		return ( UserEmplyVo ) load ( empId , "loadEmp" );
	}
	
	public List queryEmp ( UserEmplyVo userEmplyVo ) throws CheckException {
		return queryList ( userEmplyVo , "queryEmp" );
	}
	
	public List queryEmpsOfTitle ( TitleMsgVo titleMsgVo )
			throws CheckException {
		return queryList ( titleMsgVo , "queryEmpsOfTitle" );
	}
	
	public int countEmpsOfTitle ( TitleMsgVo titleMsgVo ) throws CheckException {
		return getCount( titleMsgVo , "countEmpsOfTitle" ).intValue ( ) ;
	}
	
	
	public List queryEmpsOfTitleId( Long titleId ) throws CheckException {
		return queryList ( titleId , "queryEmpsOfTitleId" );
	}
	
	public int countEmpsOfTitleId( Long titleId ) throws CheckException {
		return getCount( titleId , "countEmpsOfTitleId" ).intValue ( ) ;
	}
	
	public List queryEmpsOfTitle ( Long titleId ) throws CheckException {
		return queryList ( titleId , "queryEmpsOfTitle" );
	}
	
	public int updateEmp ( UserEmplyVo userEmplyVo ) throws CheckException {
		int result  = update ( userEmplyVo , "updateEmp" );
		if ( AppContext.isHasLog (  ) ) {	
			userEmplyVo.setOperType ( Constants.LOG_OPER_UPDATE );
			addLogAuthEmply ( userEmplyVo );
		}
		return result;
	}
	
	public void updateEmpPrimaryTitleISNULL ( Long titleId )
			throws CheckException {
		update ( titleId , "updateEmpPrimaryTitleISNULL" );
	}
	
	public void removeEmp ( Long empId , Long operuser) throws CheckException {
		if ( AppContext.isHasLog (  ) ) {	
			UserEmplyVo userEmplyVo = (UserEmplyVo)this.loadEmp ( empId );
			userEmplyVo.setOperUser ( operuser );
			userEmplyVo.setOperType ( Constants.LOG_OPER_REMOVE );
			addLogAuthEmply ( userEmplyVo );
		}
		delete ( empId , "deleteEmp" );
	}
	
	public int updateEmpPrimaryTitle ( UserEmplyVo userEmplyVo )
			throws CheckException {
		int result = update ( userEmplyVo , "updateEmpPrimaryTitle" );
		if ( AppContext.isHasLog (  ) ) {		
			EmpTitleRelVo empTitleRelVo = new EmpTitleRelVo ( );
			empTitleRelVo.setEmpId ( userEmplyVo.getEmpId ( ) );
			empTitleRelVo.setTitleId ( userEmplyVo.getTitleId ( ) );
			empTitleRelVo.setOperType ( Constants.LOG_OPER_UPDATE) ;
			empTitleRelVo.setOperUser ( userEmplyVo.getOperUser ( ) );
			addLogEmpTitleRel2 ( empTitleRelVo );
		}
		return result ;
	}
	
	public void addLogEmpTitleRel2( EmpTitleRelVo empTitleRelVo )  throws CheckException { 
		insert ( empTitleRelVo , "addLogEmpTitleRel" ) ;
	}
	
	public Long countIdNOByIdType ( UserEmplyVo userEmplyVo ) throws CheckException {
		return getCount ( userEmplyVo , "countIdNOByIdType" );
	}
	
	public List getOnlineUser ( UserEmplyVo userEmplyVo ) throws CheckException {
		return queryList ( userEmplyVo , "getOnlineUser" );
	}
	
	public void addLogAuthEmply( UserEmplyVo userEmplyVo )  throws CheckException { 
		insert ( userEmplyVo , "addLogAuthEmply" ) ;
	}
}
